package com.platform.modules.wallet.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.platform.common.exception.BaseException;
import com.platform.modules.wallet.service.WalletKPayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import java.util.TreeMap;

@Service("walletKPayService")
@Slf4j
public class WalletKPayServiceImpl implements WalletKPayService {

    String gatewayUrl = "http://63.216.193.82:8002";
    String mchId = "E000000071";
    String secret = "E3450140B3DE44FCAFE47E3DAAE9BDC3";


    @Override
    public String getWalletCode(String username, String security) {

        Map<String, Object> params = new TreeMap();

        params.put("username", username);
//        params.put("security", security);

        JSONObject jsonObject = sendPost(params, "/game/getwalletcode");

        return jsonObject.getStr("walletcode");
    }

    @Override
    public int getBalance(String walletCode) {


        Map<String, Object> params = new TreeMap();

        params.put("walletcode", walletCode);

        JSONObject jsonObject = sendPost(params, "/game/balance");


        return jsonObject.getInt("balance");
    }

    @Override
    public boolean deposit(String walletCode, BigDecimal amount, String number, String security) {

        Map<String, Object> params = new TreeMap();

        params.put("walletcode", walletCode);
        params.put("amount", amount);
        params.put("number", number);
        params.put("security", security);

        sendPost(params, "/game/deposit");

        return true;
    }

    @Override
    public boolean withdraw(String walletCode, BigDecimal amount, String number, String security) {

        Map<String, Object> params = new TreeMap();

        params.put("walletcode", walletCode);
        params.put("amount", amount);
        params.put("number", number);
//        params.put("security", security);

        sendPost(params, "/game/withdraw");

        return true;
    }

    @Override
    public String register(String phone, String name, String nick) {


        Map<String, Object> params = new TreeMap();

        params.put("phone", phone);
        params.put("name", name);
        params.put("nick", nick);

        JSONObject jsonObject = sendPost(params, "/game/register");

        return jsonObject.getStr("walletcode");
    }

    @Override
    public String login(String walletCode) {


        Map<String, Object> params = new TreeMap();
        params.put("mch_id", mchId);
        params.put("time", System.currentTimeMillis() + 28800000);
        params.put("walletcode", walletCode);

        StringBuilder sb = new StringBuilder();
        for (String key : params.keySet()) {
            sb.append(key).append("=").append(params.get(key)).append("&");
        }

        String param = sb.toString();


        String md5 = SecureUtil.md5(param + "key=" + secret);

        return "http://www.wkh797.com/home/login" + "?" + param + "sign=" + md5;
    }

    private JSONObject sendPost(Map<String, Object> params, String path) {

        params.put("mch_id", mchId);

        StringBuilder sb = new StringBuilder();
        for (String key : params.keySet()) {
            sb.append(key).append("=").append(params.get(key)).append("&");
        }

        String param = sb.toString();

        String md5 = SecureUtil.md5(param + "key=" + secret);

        log.info("请求数据{}", param + "sign=" + md5);
        params.put("sign", md5);

        String result = HttpUtil.post(gatewayUrl + path, params);

        log.info("返回数据{}", result);
        //mch_id=E000000071&result_code=0000&result_message=OK&sign=3e7f83fc4910d9cf85d6d9b4c0b964c4&sign_type=MD5&walletcode=c7e1249ffc03eb9ded908c236bd1996d
        //result_code=0004&result_message=手机号已经存在

        String[] split = result.split("&");

        Map<String, String> map = new HashedMap();
        for (String array : split) {
            String[] split1 = array.split("=");
            map.put(split1[0], split1[1]);
        }

        if (!StrUtil.equals("0000", map.get("result_code"))) {
            throw new BaseException(map.get("result_message"));
        }

        return JSONUtil.parseObj(map);
    }
}
